package com.example.demo.model.admin.admin.controler;

import com.example.demo.Results.Result;
import com.example.demo.config.RandomValidateCodeUtil;
import com.example.demo.model.admin.admin.entity.ShopAdmin;
import com.example.demo.model.admin.admin.entity.reg.LoginReg;
import com.example.demo.model.admin.admin.service.ShopAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(tags = "管理员操作")
@Slf4j
@RestController
@RequestMapping("/admin/shopAdmin")
public class ShopAdminResource {

    @Autowired
    private ShopAdminService shopAdminService;

    @GetMapping("login")
    @ApiOperation("登录页")
    public ModelAndView login(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/admin/login");
        return mv;
    }

    @GetMapping("reg")
    @ApiOperation("注册页")
    public ModelAndView reg(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/admin/reg");
        return mv;
    }

    //    生成验证码
    @GetMapping("/getValidateCode")
    @ApiOperation("生成验证码")
    public void  getValidateCode(HttpServletRequest request, HttpServletResponse response) {
        try {
            //设置相应类型,告诉浏览器输出的内容为图片
            response.setContentType("image/jpeg");
            //设置响应头信息，告诉浏览器不要缓存此内容
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
            randomValidateCode.getRandomCode(request, response);//输出验证码图片方法
        } catch (Exception e) {
            log.error("获取验证码失败>>>>   ", e);
        }
    }

    /**
     * 登录控制器
     * @param loginReg
     * @return
     */
    @ApiOperation("登录验证")
    @PostMapping("/doLogin")
    public Result doLogin(HttpServletRequest request,LoginReg loginReg){
        return  shopAdminService.doLogin(request,loginReg);
    }
    /**
     * 注册方法
     * @param loginReg
     * @return
     */
    @ApiOperation("注册验证")
    @PostMapping("/doReg")
    public Result doReg(HttpServletRequest request,LoginReg loginReg){
        return  shopAdminService.doReg(request,loginReg);
    }

    /**
     * 退出方法
     * @return
     */
    @PostMapping("/doLogout")
    @ApiOperation("退出方法")
    public Result doLogout(HttpServletRequest request){
        request.getSession().removeAttribute("adminId");
        request.getSession().removeAttribute("adminName");
        return  Result.ok("退出成功！");
    }

    /**
     * 修改密码页面
     * @return
     */
    @GetMapping("/editPass")
    @ApiOperation("修改密码页")
    public ModelAndView editPass() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/admin/editPass");
        return mv;
    }

    /**
     * 判断密码原密码是否一致
     */
    @PostMapping("/comparePass")
    @ApiOperation("判断密码")
    public Result comparePass(HttpServletRequest request,String oldPass){
        Integer adminId = (Integer) request.getSession().getAttribute("adminId");
        ShopAdmin shopAdmin = shopAdminService.getById(adminId);
        String pass = DigestUtils.md5DigestAsHex(oldPass.getBytes());
        if (shopAdmin.getAdminPass().equals(pass)){
            return Result.ok("密码正确，请继续完成修改!");
        }else{
            return Result.fail("密码不正确，请修改后再试!");
        }
    }

    /**
     * 修改密码
     * @return
     */
    @PostMapping("/editPass")
    @ApiOperation("修改密码")
    public Result editPass(HttpServletRequest request,String newPass){
        Integer adminId = (Integer) request.getSession().getAttribute("adminId");
        String pass = DigestUtils.md5DigestAsHex(newPass.getBytes());
        ShopAdmin shopAdmin = shopAdminService.getById(adminId);
        shopAdmin.setAdminPass(pass);
        boolean b = shopAdminService.updateById(shopAdmin);
        if (b){
            return  Result.ok("修改成功！");
        }else {
            return  Result.fail("修改失败！");
        }

    }
}
